﻿
using Chain.BLL;
using Chain.Common;
using Chain.Common.DEncrypt;
using Chain.Model;
using Chain.Tasks;
using Chain.DAL;
using Chain.DBUtility;
using Chain.IDAL;
using Chain.Wechat;
using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Configuration;
using System.Web.Configuration;

public partial class Global : HttpApplication
{
   
    public Hashtable HardwareID = new Hashtable();

    protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
    }

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
    }

    protected void Application_End(object sender, EventArgs e)
    {
    }

    protected void Application_Error(object sender, EventArgs e)
    {
        Exception objErr = base.Server.GetLastError().GetBaseException();
        string err = "会员系统 Caught in Application_Error event\r\nError in:" + base.Request.Url.ToString() + "\r\nError Message:" + objErr.Message.ToString() + "\r\nStack Trace:" + objErr.StackTrace.ToString();
        try
        {
            Chain.Model.SysError mdSysError = new Chain.Model.SysError();
            Chain.BLL.SysError bllSysError = new Chain.BLL.SysError();
            mdSysError.ErrorTime = DateTime.Now;
            mdSysError.ErrorContent = err;
            mdSysError.Ipaddress = PubFunction.ipAdress;
            LoginLogic login = LoginLogic.LoginStatus();
            if (login.IsLoggedOn && (login.LoginUser != null))
            {
                mdSysError.UserID = login.LoginUser.UserID;
                mdSysError.ShopID = login.LoginUser.UserShopID;
            }
            bllSysError.Add(mdSysError);
        }
        catch
        {
        }
    }

    protected void Application_Start(object sender, EventArgs e)
    {
        //string url = AppDomain.CurrentDomain.BaseDirectory;//HttpContext.Current.Request.Url.Host;


        //if (!string.IsNullOrEmpty(url) && url.ToLower().IndexOf("chainstock") > 0)
        //{
        //    string oldstr = System.Configuration.ConfigurationManager.AppSettings["ConnString"].ToString();
        //    string oldreplacestr = oldstr.Split(';')[1];
        //    if (oldreplacestr.IndexOf("ChainStock") > 0)
        //    {
        //        string newreplacestr = "database=ChainStock2";
        //        string newstr = oldstr.Replace(oldreplacestr, newreplacestr);
        //        SetConfig("ConnString", newstr);

        //    }
        //}

        

        Hashtable Online = new Hashtable();
        base.Context.Cache.Insert("Online", Online);
        base.Context.Cache["count"] = 0;
        string SafetyVerification = Chain.Common.DEncrypt.DEncrypt.EncryptDecrypt(Chain.Common.DEncrypt.DEncrypt.EncryptDecrypt(ConfigHelper.GetValue("SafetyVerification")));
        try
        {
            PubFunction.ISCheckKey = bool.Parse(SafetyVerification);
        }
        catch
        {
            PubFunction.ISCheckKey = false;
        }
        if (PubFunction.ISCheckKey)
        {
            string[] Hardware = INIFile.IniReadValue("SysConfig", "license").Split(new char[] { '-' });
            for (int i = 0; i < Hardware.Length; i++)
            {
                this.HardwareID.Add(i, Hardware[i].ToString());
            }
            base.Application["HardwareID"] = this.HardwareID;
        }

       



        DataTable db = PubFunction.GetUserAuthority(1);
        for (int i = 0; i < db.Rows.Count; i++)
        {
            db.Rows[i]["ActionControl"].ToString();
            db.Rows[i]["ActionControl"].ToString();
            if ((db.Rows[i]["ActionControl"].ToString() != "page") && (db.Rows[i]["ActionControl"].ToString() != "module"))
            {
                db.Rows[i]["ActionValue"] = "false";
            }
        }
        base.Application["Authority"] = db;
        TaskConfig.Init(base.Server.MapPath("~"));
        TaskManager.Instance.Initialize(TaskConfig.ScheduleTasks);
        TaskManager.Instance.Start();
    }

    public void LogoutCache()
    {
        Hashtable Online = (base.Context != null) ? ((Hashtable)base.Context.Cache["Online"]) : null;
        if (Online != null)
        {
            if (Online[base.Session.SessionID] != null)
            {
                Online.Remove(base.Session.SessionID);
            }
            base.Context.Cache["Online"] = Online;
        }
    }

    protected void Session_End(object sender, EventArgs e)
    {
        this.LogoutCache();
        if (base.Context != null)
        {
            base.Context.Cache["count"] = ((int)base.Context.Cache["count"]) - 1;
        }
    }

    protected void Session_Start(object sender, EventArgs e)
    {
    }

    public void SetConfig(string key,string value) {

        Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
        AppSettingsSection objAppSettings = (AppSettingsSection)config.GetSection("appSettings");
        objAppSettings.Settings[key].Value = value;
        config.Save(ConfigurationSaveMode.Modified);
    }


}


